<?php
namespace app\common\model;
use think\Db;
use think\Model;


class AdminInfo extends Model
{
    private $table_name = 'web_admin_info';


    //验证条件
    protected $rule = [
        'admin_password'   =>  'require|max:32',
        'admin_accounts'   =>  'require|unique:admin_info|max:11'

    ];

    //验证提示信息
    protected $message = [
        'admin_password.max'     => '密码最多不能超过32个字符',
        'admin_password.require' => '密码必须',
        'admin_accounts.require' => '管理员账号为必须',
        'admin_accounts.unique'  => '管理员账号已存在',
        'admin_accounts.max'     => '管理员账号不能超过11个字'

    ];

    protected $scene = [
        'edit'  =>  ['admin_accounts'],
    ];

    public function getUserInfo($_opt)
    {
        $data = $this
            ->allowField(true)
            ->where($_opt)
            ->find();
        return $data;
    }

    public function getUserPage($_opt, $field='*', $limit='10')
    {
        $data = $this->allowField(true)->where($_opt)->field($field)->paginate($limit);
        return $data;
    }



    public function addUserInfo($_opt)
    {
        $data = $this->allowField(true)->validate($this->rule,$this->message)->save($_opt);
        return $data;
    }

    public function updateInfo($_opt)
    {
        $data = $this
            ->allowField(true)
            ->validate($this->rule,$this->message)
            ->update($_opt);

        return $data;
    }

    public function deteteInfo($_opt)
    {
        $info = $this->allowField(true)->validate($this->rule,$this->message)->update($_opt);

        //$this->updateRedis('hDel', $_opt['admin_id']);

        return $info;
    }

    /**
     * 获得成员信息
     * @author Rocky Huang 2017-08-29
     * @return [type] [description]
     */
    public function getMember()
    {
        $_opt['user_status'] = array('neq',2);
        $data = $this
            ->where($_opt)
            ->field('admin_accounts')
            ->select();

        $res = [];
        if(is_array($data)){
            foreach ($data as $key => $value){
                $res[] =$value->toArray();
            }
        }
        return $res;
    }

    /**
     * 获取userName值
     * @author Rocky Huang 2018-05-14
     * @param  [type] $value [description]
     * @return [type]        [description]
     */
    public function getRedisUserInfo($accounts){
        $memberArray = $this->getUserInfoFromRedis();
        if(is_array($memberArray)){
            //循环获取数据
            foreach ($memberArray as $key => $value) {
                if($memberArray[$key]['user_accounts'] == $accounts){
                    return $memberArray[$key]['user_name'];
                }
            }
        }

        return false;
    }


}